package com.ruoyi.project.module.route.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.route.domain.Route;
import com.ruoyi.project.module.route.service.IRouteService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 业务路由 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-07-27
 */
@Controller
@RequestMapping("/module/route")
public class RouteController extends BaseController
{
    private String prefix = "module/route";
	
	@Autowired
	private IRouteService routeService;
	
	@RequiresPermissions("module:route:view")
	@GetMapping()
	public String route()
	{
	    return prefix + "/route";
	}
	
	/**
	 * 查询业务路由列表
	 */
	@RequiresPermissions("module:route:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Route route)
	{
		startPage();
		ServletUtils.getSession().setAttribute("cpeId", route.getCpeId());
        List<Route> list = routeService.selectRouteList(route);
		return getDataTable(list);
	}
	
	/**
	 * 新增业务路由
	 */
	@RequiresPermissions("module:route:add")
	@Log(title = "业务路由", action = BusinessType.INSERT)
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改业务路由
	 */
	@RequiresPermissions("module:route:edit")
	@Log(title = "业务路由", action = BusinessType.UPDATE)
	@GetMapping("/edit/{routeId}")
	public String edit(@PathVariable("routeId") Integer routeId, Model model)
	{
		Route route = routeService.selectRouteById(routeId);
		model.addAttribute("route", route);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存业务路由
	 */
	@RequiresPermissions("module:route:save")
	@Log(title = "业务路由", action = BusinessType.SAVE)
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Route route)
	{	
		int cpeId = (int) ServletUtils.getSession().getAttribute("cpeId");
		route.setCpeId(cpeId);
		if (routeService.saveRoute(route) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除业务路由
	 */
	@RequiresPermissions("module:route:remove")
	@Log(title = "业务路由", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = routeService.deleteRouteByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
}
